unit CommTreeView;

{$ALIGN ON}
{$MINENUMSIZE 4}
{$WEAKPACKAGEUNIT}

interface

uses Windows, Messages;

type
{ Common Types }
  POleStr = PWideChar;
  Largeint = Int64;
  TCLSID = TGUID;


{ IStream interface }
  IStream = interface;


{$IFDEF MSWINDOWS}
  PStatStg = ^TStatStg;
  {$EXTERNALSYM tagSTATSTG}
  tagSTATSTG = record
    pwcsName: POleStr;
    dwType: Longint;
    cbSize: Largeint;
    mtime: TFileTime;
    ctime: TFileTime;
    atime: TFileTime;
    grfMode: Longint;
    grfLocksSupported: Longint;
    clsid: TCLSID;
    grfStateBits: Longint;
    reserved: Longint;
  end;
  TStatStg = tagSTATSTG;
  {$EXTERNALSYM STATSTG}
  STATSTG = TStatStg;

  {$EXTERNALSYM ISequentialStream}
  ISequentialStream = interface(IUnknown)
    ['{0c733a30-2a1c-11ce-ade5-00aa0044773d}']
    function Read(pv: Pointer; cb: Longint; pcbRead: PLongint): HResult;
      stdcall;
    function Write(pv: Pointer; cb: Longint; pcbWritten: PLongint): HResult;
      stdcall;
  end;

  {$EXTERNALSYM IStream}
  IStream = interface(ISequentialStream)
    ['{0000000C-0000-0000-C000-000000000046}']
    function Seek(dlibMove: Largeint; dwOrigin: Longint;
      out libNewPosition: Largeint): HResult; stdcall;
    function SetSize(libNewSize: Largeint): HResult; stdcall;
    function CopyTo(stm: IStream; cb: Largeint; out cbRead: Largeint;
      out cbWritten: Largeint): HResult; stdcall;
    function Commit(grfCommitFlags: Longint): HResult; stdcall;
    function Revert: HResult; stdcall;
    function LockRegion(libOffset: Largeint; cb: Largeint;
      dwLockType: Longint): HResult; stdcall;
    function UnlockRegion(libOffset: Largeint; cb: Largeint;
      dwLockType: Longint): HResult; stdcall;
    function Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult;
      stdcall;
    function Clone(out stm: IStream): HResult; stdcall;
  end;
{$ELSE}
  PStatStg = Types.PStatStg;
  TStatStg = Types.TStatStg;
  ISequentialStream = Types.ISequentialStream;
  IStream = Types.IStream;
{$ENDIF}


const
  {$EXTERNALSYM CCM_FIRST}
  CCM_FIRST               = $2000;      { Common control shared messages }


  {$EXTERNALSYM CCM_SETUNICODEFORMAT}
  CCM_SETUNICODEFORMAT    = CCM_FIRST + 5;
  {$EXTERNALSYM CCM_GETUNICODEFORMAT}
  CCM_GETUNICODEFORMAT    = CCM_FIRST + 6;



{ ==================== CUSTOM DRAW ========================================== }

const
  // custom draw return flags
  // values under 0x00010000 are reserved for global custom draw values.
  // above that are for specific controls
  {$EXTERNALSYM CDRF_DODEFAULT}
  CDRF_DODEFAULT          = $00000000;
  {$EXTERNALSYM CDRF_NEWFONT}
  CDRF_NEWFONT            = $00000002;
  {$EXTERNALSYM CDRF_SKIPDEFAULT}
  CDRF_SKIPDEFAULT        = $00000004;

  {$EXTERNALSYM CDRF_NOTIFYPOSTPAINT}
  CDRF_NOTIFYPOSTPAINT    = $00000010;
  {$EXTERNALSYM CDRF_NOTIFYITEMDRAW}
  CDRF_NOTIFYITEMDRAW     = $00000020;
  {$EXTERNALSYM CDRF_NOTIFYSUBITEMDRAW}
  CDRF_NOTIFYSUBITEMDRAW  = $00000020;  // flags are the same, we can distinguish by context
  {$EXTERNALSYM CDRF_NOTIFYPOSTERASE}
  CDRF_NOTIFYPOSTERASE    = $00000040;

  // drawstage flags
  // values under = $00010000 are reserved for global custom draw values.
  // above that are for specific controls
  {$EXTERNALSYM CDDS_PREPAINT}
  CDDS_PREPAINT           = $00000001;
  {$EXTERNALSYM CDDS_POSTPAINT}
  CDDS_POSTPAINT          = $00000002;
  {$EXTERNALSYM CDDS_PREERASE}
  CDDS_PREERASE           = $00000003;
  {$EXTERNALSYM CDDS_POSTERASE}
  CDDS_POSTERASE          = $00000004;
  // the = $000010000 bit means it's individual item specific
  {$EXTERNALSYM CDDS_ITEM}
  CDDS_ITEM               = $00010000;
  {$EXTERNALSYM CDDS_ITEMPREPAINT}
  CDDS_ITEMPREPAINT       = CDDS_ITEM or CDDS_PREPAINT;
  {$EXTERNALSYM CDDS_ITEMPOSTPAINT}
  CDDS_ITEMPOSTPAINT      = CDDS_ITEM or CDDS_POSTPAINT;
  {$EXTERNALSYM CDDS_ITEMPREERASE}
  CDDS_ITEMPREERASE       = CDDS_ITEM or CDDS_PREERASE;
  {$EXTERNALSYM CDDS_ITEMPOSTERASE}
  CDDS_ITEMPOSTERASE      = CDDS_ITEM or CDDS_POSTERASE;
  {$EXTERNALSYM CDDS_SUBITEM}
  CDDS_SUBITEM            = $00020000;

  // itemState flags
  {$EXTERNALSYM CDIS_SELECTED}
  CDIS_SELECTED       = $0001;
  {$EXTERNALSYM CDIS_GRAYED}
  CDIS_GRAYED         = $0002;
  {$EXTERNALSYM CDIS_DISABLED}
  CDIS_DISABLED       = $0004;
  {$EXTERNALSYM CDIS_CHECKED}
  CDIS_CHECKED        = $0008;
  {$EXTERNALSYM CDIS_FOCUS}
  CDIS_FOCUS          = $0010;
  {$EXTERNALSYM CDIS_DEFAULT}
  CDIS_DEFAULT        = $0020;
  {$EXTERNALSYM CDIS_HOT}
  CDIS_HOT            = $0040;
  {$EXTERNALSYM CDIS_MARKED}
  CDIS_MARKED         = $0080;
  {$EXTERNALSYM CDIS_INDETERMINATE}
  CDIS_INDETERMINATE  = $0100;
  {$EXTERNALSYM CDIS_SHOWKEYBOARDCUES}
  CDIS_SHOWKEYBOARDCUES = $0200;

type
  {$EXTERNALSYM tagNMCUSTOMDRAWINFO}
  tagNMCUSTOMDRAWINFO = packed record
    hdr: TNMHdr;
    dwDrawStage: DWORD;
    hdc: HDC;
    rc: TRect;
    dwItemSpec: DWORD;  // this is control specific, but it's how to specify an item.  valid only with CDDS_ITEM bit set
    uItemState: UINT;
    lItemlParam: LPARAM;
  end;
  PNMCustomDraw = ^TNMCustomDraw;
  TNMCustomDraw = tagNMCUSTOMDRAWINFO;

  {$EXTERNALSYM tagNMTTCUSTOMDRAW}
  tagNMTTCUSTOMDRAW = packed record
    nmcd: TNMCustomDraw;
    uDrawFlags: UINT;
  end;
  PNMTTCustomDraw = ^TNMTTCustomDraw;
  TNMTTCustomDraw = tagNMTTCUSTOMDRAW;


{ ====== IMAGE LIST =========================================== }

const
  {$EXTERNALSYM CLR_NONE}
  CLR_NONE                = $FFFFFFFF;
  {$EXTERNALSYM CLR_DEFAULT}
  CLR_DEFAULT             = $FF000000;

type
  {$EXTERNALSYM HIMAGELIST}
  HIMAGELIST = THandle;

  {$EXTERNALSYM _IMAGELISTDRAWPARAMS}
  _IMAGELISTDRAWPARAMS = packed record
    cbSize: DWORD;
    himl: HIMAGELIST;
    i: Integer;
    hdcDst: HDC;
    x: Integer;
    y: Integer;
    cx: Integer;
    cy: Integer;
    xBitmap: Integer;        // x offest from the upperleft of bitmap
    yBitmap: Integer;        // y offset from the upperleft of bitmap
    rgbBk: COLORREF;
    rgbFg: COLORREF;
    fStyle: UINT;
    dwRop: DWORD;
  end;
  PImageListDrawParams = ^TImageListDrawParams;
  TImageListDrawParams = _IMAGELISTDRAWPARAMS;

const
  {$EXTERNALSYM ILC_MASK}
  ILC_MASK                = $0001;
  {$EXTERNALSYM ILC_COLOR}
  ILC_COLOR               = $0000;
  {$EXTERNALSYM ILC_COLORDDB}
  ILC_COLORDDB            = $00FE;
  {$EXTERNALSYM ILC_COLOR4}
  ILC_COLOR4              = $0004;
  {$EXTERNALSYM ILC_COLOR8}
  ILC_COLOR8              = $0008;
  {$EXTERNALSYM ILC_COLOR16}
  ILC_COLOR16             = $0010;
  {$EXTERNALSYM ILC_COLOR24}
  ILC_COLOR24             = $0018;
  {$EXTERNALSYM ILC_COLOR32}
  ILC_COLOR32             = $0020;
  {$EXTERNALSYM ILC_PALETTE}
  ILC_PALETTE             = $0800;

{$EXTERNALSYM ImageList_Create}
function ImageList_Create(CX, CY: Integer; Flags: UINT;
  Initial, Grow: Integer): HIMAGELIST; stdcall;
{$EXTERNALSYM ImageList_Destroy}
function ImageList_Destroy(ImageList: HIMAGELIST): Bool; stdcall;
{$EXTERNALSYM ImageList_GetImageCount}
function ImageList_GetImageCount(ImageList: HIMAGELIST): Integer; stdcall;
{$EXTERNALSYM ImageList_SetImageCount}
function ImageList_SetImageCount(himl: HIMAGELIST; uNewCount: UINT): Integer; stdcall;
{$EXTERNALSYM ImageList_Add}
function ImageList_Add(ImageList: HIMAGELIST; Image, Mask: HBitmap): Integer; stdcall;
{$EXTERNALSYM ImageList_ReplaceIcon}
function ImageList_ReplaceIcon(ImageList: HIMAGELIST; Index: Integer;
  Icon: HIcon): Integer; stdcall;
{$EXTERNALSYM ImageList_SetBkColor}
function ImageList_SetBkColor(ImageList: HIMAGELIST; ClrBk: TColorRef): TColorRef; stdcall;
{$EXTERNALSYM ImageList_GetBkColor}
function ImageList_GetBkColor(ImageList: HIMAGELIST): TColorRef; stdcall;
{$EXTERNALSYM ImageList_SetOverlayImage}
function ImageList_SetOverlayImage(ImageList: HIMAGELIST; Image: Integer;
  Overlay: Integer): Bool; stdcall;

{$EXTERNALSYM ImageList_AddIcon}
function ImageList_AddIcon(ImageList: HIMAGELIST; Icon: HIcon): Integer; inline;

const
  {$EXTERNALSYM ILD_NORMAL}
  ILD_NORMAL              = $0000;
  {$EXTERNALSYM ILD_TRANSPARENT}
  ILD_TRANSPARENT         = $0001;
  {$EXTERNALSYM ILD_MASK}
  ILD_MASK                = $0010;
  {$EXTERNALSYM ILD_IMAGE}
  ILD_IMAGE               = $0020;
  {$EXTERNALSYM ILD_ROP}
  ILD_ROP                 = $0040;
  {$EXTERNALSYM ILD_BLEND25}
  ILD_BLEND25             = $0002;
  {$EXTERNALSYM ILD_BLEND50}
  ILD_BLEND50             = $0004;
  {$EXTERNALSYM ILD_OVERLAYMASK}
  ILD_OVERLAYMASK         = $0F00;

{$EXTERNALSYM IndexToOverlayMask}
function IndexToOverlayMask(Index: Integer): Integer; inline;

const
  {$EXTERNALSYM ILD_SELECTED}
  ILD_SELECTED            = ILD_BLEND50;
  {$EXTERNALSYM ILD_FOCUS}
  ILD_FOCUS               = ILD_BLEND25;
  {$EXTERNALSYM ILD_BLEND}
  ILD_BLEND               = ILD_BLEND50;
  {$EXTERNALSYM CLR_HILIGHT}
  CLR_HILIGHT             = CLR_DEFAULT;

{$EXTERNALSYM ImageList_Draw}
function ImageList_Draw(ImageList: HIMAGELIST; Index: Integer;
  Dest: HDC; X, Y: Integer; Style: UINT): Bool; stdcall;

{$EXTERNALSYM ImageList_Replace}
function ImageList_Replace(ImageList: HIMAGELIST; Index: Integer;
  Image, Mask: HBitmap): Bool; stdcall;
{$EXTERNALSYM ImageList_AddMasked}
function ImageList_AddMasked(ImageList: HIMAGELIST; Image: HBitmap;
  Mask: TColorRef): Integer; stdcall;
{$EXTERNALSYM ImageList_DrawEx}
function ImageList_DrawEx(ImageList: HIMAGELIST; Index: Integer;
  Dest: HDC; X, Y, DX, DY: Integer; Bk, Fg: TColorRef; Style: Cardinal): Bool; stdcall;
{$EXTERNALSYM ImageList_DrawIndirect}
function ImageList_DrawIndirect(pimldp: PImageListDrawParams): Integer; stdcall;
{$EXTERNALSYM ImageList_Remove}
function ImageList_Remove(ImageList: HIMAGELIST; Index: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_GetIcon}
function ImageList_GetIcon(ImageList: HIMAGELIST; Index: Integer;
  Flags: Cardinal): HIcon; stdcall;
{$EXTERNALSYM ImageList_LoadImage}
function ImageList_LoadImage(Instance: THandle; Bmp: PChar; CX, Grow: Integer;
  Mask: TColorRef; pType, Flags: Cardinal): HIMAGELIST; stdcall;
{$EXTERNALSYM ImageList_LoadImageA}
function ImageList_LoadImageA(Instance: THandle; Bmp: PAnsiChar; CX, Grow: Integer;
  Mask: TColorRef; pType, Flags: Cardinal): HIMAGELIST; stdcall;
{$EXTERNALSYM ImageList_LoadImageW}
function ImageList_LoadImageW(Instance: THandle; Bmp: PWideChar; CX, Grow: Integer;
  Mask: TColorRef; pType, Flags: Cardinal): HIMAGELIST; stdcall;

const
  {$EXTERNALSYM ILCF_MOVE}
  ILCF_MOVE   = $00000000;
  {$EXTERNALSYM ILCF_SWAP}
  ILCF_SWAP   = $00000001;

{$EXTERNALSYM ImageList_Copy}
function ImageList_Copy(himlDst: HIMAGELIST; iDst: Integer; himlSrc: HIMAGELIST;
  Src: Integer; uFlags: UINT): Integer; stdcall;

{$EXTERNALSYM ImageList_BeginDrag}
function ImageList_BeginDrag(ImageList: HIMAGELIST; Track: Integer;
  XHotSpot, YHotSpot: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_EndDrag}
function ImageList_EndDrag: Bool; stdcall;
{$EXTERNALSYM ImageList_DragEnter}
function ImageList_DragEnter(LockWnd: HWnd; X, Y: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_DragLeave}
function ImageList_DragLeave(LockWnd: HWnd): Bool; stdcall;
{$EXTERNALSYM ImageList_DragMove}
function ImageList_DragMove(X, Y: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_SetDragCursorImage}
function ImageList_SetDragCursorImage(ImageList: HIMAGELIST; Drag: Integer;
  XHotSpot, YHotSpot: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_DragShowNolock}
function ImageList_DragShowNolock(Show: Bool): Bool; stdcall;
{$EXTERNALSYM ImageList_GetDragImage}
function ImageList_GetDragImage(Point, HotSpot: PPoint): HIMAGELIST; stdcall;

{ macros }
{$EXTERNALSYM ImageList_RemoveAll}
procedure ImageList_RemoveAll(ImageList: HIMAGELIST); inline;
{$EXTERNALSYM ImageList_ExtractIcon}
function ImageList_ExtractIcon(Instance: THandle; ImageList: HIMAGELIST;
  Image: Integer): HIcon; inline;
{$EXTERNALSYM ImageList_LoadBitmap}
function ImageList_LoadBitmap(Instance: THandle; Bmp: PChar;
  CX, Grow: Integer; MasK: TColorRef): HIMAGELIST;
{$EXTERNALSYM ImageList_LoadBitmapA}
function ImageList_LoadBitmapA(Instance: THandle; Bmp: PAnsiChar;
  CX, Grow: Integer; MasK: TColorRef): HIMAGELIST;
{$EXTERNALSYM ImageList_LoadBitmapW}
function ImageList_LoadBitmapW(Instance: THandle; Bmp: PWideChar;
  CX, Grow: Integer; MasK: TColorRef): HIMAGELIST;

{$EXTERNALSYM ImageList_Read}
function ImageList_Read(Stream: IStream): HIMAGELIST; stdcall;
{$EXTERNALSYM ImageList_Write}
function ImageList_Write(ImageList: HIMAGELIST; Stream: IStream): BOOL; stdcall;

type
  PImageInfo = ^TImageInfo;
  {$EXTERNALSYM _IMAGEINFO}
  _IMAGEINFO = packed record
    hbmImage: HBitmap;
    hbmMask: HBitmap;
    Unused1: Integer;
    Unused2: Integer;
    rcImage: TRect;
  end;
  TImageInfo = _IMAGEINFO;
  {$EXTERNALSYM IMAGEINFO}
  IMAGEINFO = _IMAGEINFO;

{$EXTERNALSYM ImageList_GetIconSize}
function ImageList_GetIconSize(ImageList: HIMAGELIST; var CX, CY: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_SetIconSize}
function ImageList_SetIconSize(ImageList: HIMAGELIST; CX, CY: Integer): Bool; stdcall;
{$EXTERNALSYM ImageList_GetImageInfo}
function ImageList_GetImageInfo(ImageList: HIMAGELIST; Index: Integer;
  var ImageInfo: TImageInfo): Bool; stdcall;
{$EXTERNALSYM ImageList_Merge}
function ImageList_Merge(ImageList1: HIMAGELIST; Index1: Integer;
  ImageList2: HIMAGELIST; Index2: Integer; DX, DY: Integer): HIMAGELIST; stdcall;
{$EXTERNALSYM ImageList_Duplicate}
function ImageList_Duplicate(himl: HIMAGELIST): HIMAGELIST; stdcall;


const
  {$EXTERNALSYM ICC_TREEVIEW_CLASSES}
  ICC_TREEVIEW_CLASSES   = $00000002; // treeview, tooltips

  {$EXTERNALSYM TV_FIRST}
  TV_FIRST                = $1100;      { TreeView messages }

  {$EXTERNALSYM TVN_FIRST}
  TVN_FIRST                = 0-400;       { treeview }
  {$EXTERNALSYM TVN_LAST}
  TVN_LAST                 = 0-499;

{ ====== TREEVIEW CONTROL =================== }

const
  {$EXTERNALSYM WC_TREEVIEW}
  WC_TREEVIEW = 'SysTreeView32';

const
  {$EXTERNALSYM TVS_HASBUTTONS}
  TVS_HASBUTTONS          = $0001;
  {$EXTERNALSYM TVS_HASLINES}
  TVS_HASLINES            = $0002;
  {$EXTERNALSYM TVS_LINESATROOT}
  TVS_LINESATROOT         = $0004;
  {$EXTERNALSYM TVS_EDITLABELS}
  TVS_EDITLABELS          = $0008;
  {$EXTERNALSYM TVS_DISABLEDRAGDROP}
  TVS_DISABLEDRAGDROP     = $0010;
  {$EXTERNALSYM TVS_SHOWSELALWAYS}
  TVS_SHOWSELALWAYS       = $0020;
  {$EXTERNALSYM TVS_RTLREADING}
  TVS_RTLREADING          = $0040;
  {$EXTERNALSYM TVS_NOTOOLTIPS}
  TVS_NOTOOLTIPS          = $0080;
  {$EXTERNALSYM TVS_CHECKBOXES}
  TVS_CHECKBOXES          = $0100;
  {$EXTERNALSYM TVS_TRACKSELECT}
  TVS_TRACKSELECT         = $0200;
  {$EXTERNALSYM TVS_SINGLEEXPAND}
  TVS_SINGLEEXPAND        = $0400;
  {$EXTERNALSYM TVS_INFOTIP}
  TVS_INFOTIP             = $0800;
  {$EXTERNALSYM TVS_FULLROWSELECT}
  TVS_FULLROWSELECT       = $1000;
  {$EXTERNALSYM TVS_NOSCROLL}
  TVS_NOSCROLL            = $2000;
  {$EXTERNALSYM TVS_NONEVENHEIGHT}
  TVS_NONEVENHEIGHT       = $4000;

type
  {$EXTERNALSYM HTREEITEM}
  HTREEITEM = ^_TREEITEM;
  {$EXTERNALSYM _TREEITEM}
  _TREEITEM = packed record
  end;

const
  {$EXTERNALSYM TVIF_TEXT}
  TVIF_TEXT               = $0001;
  {$EXTERNALSYM TVIF_IMAGE}
  TVIF_IMAGE              = $0002;
  {$EXTERNALSYM TVIF_PARAM}
  TVIF_PARAM              = $0004;
  {$EXTERNALSYM TVIF_STATE}
  TVIF_STATE              = $0008;
  {$EXTERNALSYM TVIF_HANDLE}
  TVIF_HANDLE             = $0010;
  {$EXTERNALSYM TVIF_SELECTEDIMAGE}
  TVIF_SELECTEDIMAGE      = $0020;
  {$EXTERNALSYM TVIF_CHILDREN}
  TVIF_CHILDREN           = $0040;
  {$EXTERNALSYM TVIF_INTEGRAL}
  TVIF_INTEGRAL           = $0080;

  {$EXTERNALSYM TVIS_FOCUSED}
  TVIS_FOCUSED            = $0001;
  {$EXTERNALSYM TVIS_SELECTED}
  TVIS_SELECTED           = $0002;
  {$EXTERNALSYM TVIS_CUT}
  TVIS_CUT                = $0004;
  {$EXTERNALSYM TVIS_DROPHILITED}
  TVIS_DROPHILITED        = $0008;
  {$EXTERNALSYM TVIS_BOLD}
  TVIS_BOLD               = $0010;
  {$EXTERNALSYM TVIS_EXPANDED}
  TVIS_EXPANDED           = $0020;
  {$EXTERNALSYM TVIS_EXPANDEDONCE}
  TVIS_EXPANDEDONCE       = $0040;
  {$EXTERNALSYM TVIS_EXPANDPARTIAL}
  TVIS_EXPANDPARTIAL      = $0080;

  {$EXTERNALSYM TVIS_OVERLAYMASK}
  TVIS_OVERLAYMASK        = $0F00;
  {$EXTERNALSYM TVIS_STATEIMAGEMASK}
  TVIS_STATEIMAGEMASK     = $F000;
  {$EXTERNALSYM TVIS_USERMASK}
  TVIS_USERMASK           = $F000;


const
  {$EXTERNALSYM I_CHILDRENCALLBACK}
  I_CHILDRENCALLBACK  = -1;

type
  PTVItemA = ^TTVItemA;
  PTVItemW = ^TTVItemW;
  PTVItem = PTVItemA;
  {$EXTERNALSYM tagTVITEMA}
  tagTVITEMA = packed record
    mask: UINT;
    hItem: HTreeItem;
    state: UINT;
    stateMask: UINT;
    pszText: PAnsiChar;
    cchTextMax: Integer;
    iImage: Integer;
    iSelectedImage: Integer;
    cChildren: Integer;
    lParam: LPARAM;
  end;
  {$EXTERNALSYM tagTVITEMW}
  tagTVITEMW = packed record
    mask: UINT;
    hItem: HTreeItem;
    state: UINT;
    stateMask: UINT;
    pszText: PWideChar;
    cchTextMax: Integer;
    iImage: Integer;
    iSelectedImage: Integer;
    cChildren: Integer;
    lParam: LPARAM;
  end;
  {$EXTERNALSYM tagTVITEM}
  tagTVITEM = tagTVITEMA;
  {$EXTERNALSYM _TV_ITEMA}
  _TV_ITEMA = tagTVITEMA;
  {$EXTERNALSYM _TV_ITEMW}
  _TV_ITEMW = tagTVITEMW;
  {$EXTERNALSYM _TV_ITEM}
  _TV_ITEM = _TV_ITEMA;
  TTVItemA = tagTVITEMA;
  TTVItemW = tagTVITEMW;
  TTVItem = TTVItemA;
  {$EXTERNALSYM TV_ITEMA}
  TV_ITEMA = tagTVITEMA;
  {$EXTERNALSYM TV_ITEMW}
  TV_ITEMW = tagTVITEMW;
  {$EXTERNALSYM TV_ITEM}
  TV_ITEM = TV_ITEMA;

  // only used for Get and Set messages.  no notifies
  {$EXTERNALSYM tagTVITEMEXA}
  tagTVITEMEXA = packed record
    mask: UINT;
    hItem: HTREEITEM;
    state: UINT;
    stateMask: UINT;
    pszText: PAnsiChar;
    cchTextMax: Integer;
    iImage: Integer;
    iSelectedImage: Integer;
    cChildren: Integer;
    lParam: LPARAM;
    iIntegral: Integer;
  end;
  {$EXTERNALSYM tagTVITEMEXW}
  tagTVITEMEXW = packed record
    mask: UINT;
    hItem: HTREEITEM;
    state: UINT;
    stateMask: UINT;
    pszText: PWideChar;
    cchTextMax: Integer;
    iImage: Integer;
    iSelectedImage: Integer;
    cChildren: Integer;
    lParam: LPARAM;
    iIntegral: Integer;
  end;
  {$EXTERNALSYM tagTVITEMEX}
  tagTVITEMEX = tagTVITEMEXA;
  PTVItemExA = ^TTVItemExA;
  PTVItemExW = ^TTVItemExW;
  PTVItemEx = PTVItemExA;
  TTVItemExA = tagTVITEMEXA;
  TTVItemExW = tagTVITEMEXW;
  TTVItemEx = TTVItemExA;

const
  {$EXTERNALSYM TVI_ROOT}
  TVI_ROOT                = HTreeItem($FFFF0000);
  {$EXTERNALSYM TVI_FIRST}
  TVI_FIRST               = HTreeItem($FFFF0001);
  {$EXTERNALSYM TVI_LAST}
  TVI_LAST                = HTreeItem($FFFF0002);
  {$EXTERNALSYM TVI_SORT}
  TVI_SORT                = HTreeItem($FFFF0003);

type
  PTVInsertStructA = ^TTVInsertStructA;
  PTVInsertStructW = ^TTVInsertStructW;
  PTVInsertStruct = PTVInsertStructA;
  {$EXTERNALSYM tagTVINSERTSTRUCTA}
  tagTVINSERTSTRUCTA = packed record
    hParent: HTreeItem;
    hInsertAfter: HTreeItem;
    case Integer of
      0: (itemex: TTVItemExA);
      1: (item: TTVItemA);
  end;
  {$EXTERNALSYM tagTVINSERTSTRUCTW}
  tagTVINSERTSTRUCTW = packed record
    hParent: HTreeItem;
    hInsertAfter: HTreeItem;
    case Integer of
      0: (itemex: TTVItemExW);
      1: (item: TTVItemW);
  end;
  {$EXTERNALSYM tagTVINSERTSTRUCT}
  tagTVINSERTSTRUCT = tagTVINSERTSTRUCTA;
  {$EXTERNALSYM _TV_INSERTSTRUCTA}
  _TV_INSERTSTRUCTA = tagTVINSERTSTRUCTA;
  {$EXTERNALSYM _TV_INSERTSTRUCTW}
  _TV_INSERTSTRUCTW = tagTVINSERTSTRUCTW;
  {$EXTERNALSYM _TV_INSERTSTRUCT}
  _TV_INSERTSTRUCT = _TV_INSERTSTRUCTA;
  TTVInsertStructA = tagTVINSERTSTRUCTA;
  TTVInsertStructW = tagTVINSERTSTRUCTW;
  TTVInsertStruct = TTVInsertStructA;
  {$EXTERNALSYM TV_INSERTSTRUCTA}
  TV_INSERTSTRUCTA = tagTVINSERTSTRUCTA;
  {$EXTERNALSYM TV_INSERTSTRUCTW}
  TV_INSERTSTRUCTW = tagTVINSERTSTRUCTW;
  {$EXTERNALSYM TV_INSERTSTRUCT}
  TV_INSERTSTRUCT = TV_INSERTSTRUCTA;

const
  {$EXTERNALSYM TVM_INSERTITEMA}
  TVM_INSERTITEMA          = TV_FIRST + 0;
  {$EXTERNALSYM TVM_INSERTITEMW}
  TVM_INSERTITEMW          = TV_FIRST + 50;




  {$EXTERNALSYM TVM_INSERTITEM}
  TVM_INSERTITEM          = TVM_INSERTITEMA;


{$EXTERNALSYM TreeView_InsertItem}
function TreeView_InsertItem(hwnd: HWND; const lpis: TTVInsertStruct): HTreeItem; inline;
function TreeView_InsertItemA(hwnd: HWND; const lpis: TTVInsertStructA): HTreeItem; inline;
function TreeView_InsertItemW(hwnd: HWND; const lpis: TTVInsertStructW): HTreeItem; inline;

const
  {$EXTERNALSYM TVM_DELETEITEM}
  TVM_DELETEITEM          = TV_FIRST + 1;

{$EXTERNALSYM TreeView_DeleteItem}
function TreeView_DeleteItem(hwnd: HWND; hitem: HTreeItem): Bool; inline;

{$EXTERNALSYM TreeView_DeleteAllItems}
function TreeView_DeleteAllItems(hwnd: HWND): Bool; inline;

const
  {$EXTERNALSYM TVM_EXPAND}
  TVM_EXPAND              = TV_FIRST + 2;

{$EXTERNALSYM TreeView_Expand}
function TreeView_Expand(hwnd: HWND; hitem: HTreeItem; code: Integer): Bool; inline;

const
  {$EXTERNALSYM TVE_COLLAPSE}
  TVE_COLLAPSE            = $0001;
  {$EXTERNALSYM TVE_EXPAND}
  TVE_EXPAND              = $0002;
  {$EXTERNALSYM TVE_TOGGLE}
  TVE_TOGGLE              = $0003;
  {$EXTERNALSYM TVE_EXPANDPARTIAL}
  TVE_EXPANDPARTIAL       = $4000;
  {$EXTERNALSYM TVE_COLLAPSERESET}
  TVE_COLLAPSERESET       = $8000;

const
  {$EXTERNALSYM TVM_GETITEMRECT}
  TVM_GETITEMRECT         = TV_FIRST + 4;

{$EXTERNALSYM TreeView_GetItemRect}
function TreeView_GetItemRect(hwnd: HWND; hitem: HTreeItem;
  var prc: TRect; code: Bool): Bool;

const
  {$EXTERNALSYM TVM_GETCOUNT}
  TVM_GETCOUNT            = TV_FIRST + 5;

{$EXTERNALSYM TreeView_GetCount}
function TreeView_GetCount(hwnd: HWND): UINT; inline;

const
  {$EXTERNALSYM TVM_GETINDENT}
  TVM_GETINDENT           = TV_FIRST + 6;

{$EXTERNALSYM TreeView_GetIndent}
function TreeView_GetIndent(hwnd: HWND): UINT; inline;

const
  {$EXTERNALSYM TVM_SETINDENT}
  TVM_SETINDENT           = TV_FIRST + 7;

{$EXTERNALSYM TreeView_SetIndent}
function TreeView_SetIndent(hwnd: HWND; indent: Integer): Bool; inline;

const
  {$EXTERNALSYM TVM_GETIMAGELIST}
  TVM_GETIMAGELIST        = TV_FIRST + 8;

{$EXTERNALSYM TreeView_GetImageList}
function TreeView_GetImageList(hwnd: HWND; iImage: Integer): HIMAGELIST; inline;

const
  {$EXTERNALSYM TVSIL_NORMAL}
  TVSIL_NORMAL            = 0;
  {$EXTERNALSYM TVSIL_STATE}
  TVSIL_STATE             = 2;


const
  {$EXTERNALSYM TVM_SETIMAGELIST}
  TVM_SETIMAGELIST        = TV_FIRST + 9;

{$EXTERNALSYM TreeView_SetImageList}
function TreeView_SetImageList(hwnd: HWND; himl: HIMAGELIST;
  iImage: Integer): HIMAGELIST; inline;

const
  {$EXTERNALSYM TVM_GETNEXTITEM}
  TVM_GETNEXTITEM         = TV_FIRST + 10;

{$EXTERNALSYM TreeView_GetNextItem}
function TreeView_GetNextItem(hwnd: HWND; hitem: HTreeItem;
  code: Integer): HTreeItem; inline;

const
  {$EXTERNALSYM TVGN_ROOT}
  TVGN_ROOT               = $0000;
  {$EXTERNALSYM TVGN_NEXT}
  TVGN_NEXT               = $0001;
  {$EXTERNALSYM TVGN_PREVIOUS}
  TVGN_PREVIOUS           = $0002;
  {$EXTERNALSYM TVGN_PARENT}
  TVGN_PARENT             = $0003;
  {$EXTERNALSYM TVGN_CHILD}
  TVGN_CHILD              = $0004;
  {$EXTERNALSYM TVGN_FIRSTVISIBLE}
  TVGN_FIRSTVISIBLE       = $0005;
  {$EXTERNALSYM TVGN_NEXTVISIBLE}
  TVGN_NEXTVISIBLE        = $0006;
  {$EXTERNALSYM TVGN_PREVIOUSVISIBLE}
  TVGN_PREVIOUSVISIBLE    = $0007;
  {$EXTERNALSYM TVGN_DROPHILITE}
  TVGN_DROPHILITE         = $0008;
  {$EXTERNALSYM TVGN_CARET}
  TVGN_CARET              = $0009;
  {$EXTERNALSYM TVGN_LASTVISIBLE}
  TVGN_LASTVISIBLE        = $000A;

{$EXTERNALSYM TreeView_GetChild}
function TreeView_GetChild(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetNextSibling}
function TreeView_GetNextSibling(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetPrevSibling}
function TreeView_GetPrevSibling(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetParent}
function TreeView_GetParent(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetFirstVisible}
function TreeView_GetFirstVisible(hwnd: HWND): HTreeItem;
{$EXTERNALSYM TreeView_GetNextVisible}
function TreeView_GetNextVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetPrevVisible}
function TreeView_GetPrevVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_GetSelection}
function TreeView_GetSelection(hwnd: HWND): HTreeItem;
{$EXTERNALSYM TreeView_GetDropHilite}
function TreeView_GetDropHilite(hwnd: HWND): HTreeItem;
{$EXTERNALSYM TreeView_GetRoot}
function TreeView_GetRoot(hwnd: HWND): HTreeItem;
{$EXTERNALSYM TreeView_GetLastVisible}
function TreeView_GetLastVisible(hwnd: HWND): HTreeItem;

const
  {$EXTERNALSYM TVM_SELECTITEM}
  TVM_SELECTITEM          = TV_FIRST + 11;

{$EXTERNALSYM TreeView_Select}
function TreeView_Select(hwnd: HWND; hitem: HTreeItem;
  code: Integer): HTreeItem; inline;

{$EXTERNALSYM TreeView_SelectItem}
function TreeView_SelectItem(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_SelectDropTarget}
function TreeView_SelectDropTarget(hwnd: HWND; hitem: HTreeItem): HTreeItem;
{$EXTERNALSYM TreeView_SelectSetFirstVisible}
function TreeView_SelectSetFirstVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;

const
  {$EXTERNALSYM TVM_GETITEMA}
  TVM_GETITEMA             = TV_FIRST + 12;
  {$EXTERNALSYM TVM_GETITEMW}
  TVM_GETITEMW             = TV_FIRST + 62;




  {$EXTERNALSYM TVM_GETITEM}
  TVM_GETITEM             = TVM_GETITEMA;


{$EXTERNALSYM TreeView_GetItem}
function TreeView_GetItem(hwnd: HWND; var pitem: TTVItem): Bool; inline;
{$EXTERNALSYM TreeView_GetItemA}
function TreeView_GetItemA(hwnd: HWND; var pitem: TTVItemA): Bool; inline;
{$EXTERNALSYM TreeView_GetItemW}
function TreeView_GetItemW(hwnd: HWND; var pitem: TTVItemW): Bool; inline;

const
  {$EXTERNALSYM TVM_SETITEMA}
  TVM_SETITEMA             = TV_FIRST + 13;
  {$EXTERNALSYM TVM_SETITEMW}
  TVM_SETITEMW             = TV_FIRST + 63;




  {$EXTERNALSYM TVM_SETITEM}
  TVM_SETITEM             = TVM_SETITEMA;


{$EXTERNALSYM TreeView_SetItem}
function TreeView_SetItem(hwnd: HWND; const pitem: TTVItem): Bool; inline;
{$EXTERNALSYM TreeView_SetItemA}
function TreeView_SetItemA(hwnd: HWND; const pitem: TTVItemA): Bool; inline;
{$EXTERNALSYM TreeView_SetItemW}
function TreeView_SetItemW(hwnd: HWND; const pitem: TTVItemW): Bool; inline;

const
  {$EXTERNALSYM TVM_EDITLABELA}
  TVM_EDITLABELA           = TV_FIRST + 14;
  {$EXTERNALSYM TVM_EDITLABELW}
  TVM_EDITLABELW           = TV_FIRST + 65;




  {$EXTERNALSYM TVM_EDITLABEL}
  TVM_EDITLABEL           = TVM_EDITLABELA;


{$EXTERNALSYM TreeView_EditLabel}
function TreeView_EditLabel(hwnd: HWND; hitem: HTreeItem): HWND; inline;
{$EXTERNALSYM TreeView_EditLabelA}
function TreeView_EditLabelA(hwnd: HWND; hitem: HTreeItem): HWND; inline;
{$EXTERNALSYM TreeView_EditLabelW}
function TreeView_EditLabelW(hwnd: HWND; hitem: HTreeItem): HWND; inline;

const
  {$EXTERNALSYM TVM_GETEDITCONTROL}
  TVM_GETEDITCONTROL      = TV_FIRST + 15;

{$EXTERNALSYM TreeView_GetEditControl}
function TreeView_GetEditControl(hwnd: HWND): HWND; inline;


const
  {$EXTERNALSYM TVM_GETVISIBLECOUNT}
  TVM_GETVISIBLECOUNT     = TV_FIRST + 16;

{$EXTERNALSYM TreeView_GetVisibleCount}
function TreeView_GetVisibleCount(hwnd: HWND): UINT; inline;

const
  {$EXTERNALSYM TVM_HITTEST}
  TVM_HITTEST             = TV_FIRST + 17;

type
  PTVHitTestInfo = ^TTVHitTestInfo;
  {$EXTERNALSYM tagTVHITTESTINFO}
  tagTVHITTESTINFO = packed record
    pt: TPoint;
    flags: UINT;
    hItem: HTreeItem;
  end;
  {$EXTERNALSYM _TV_HITTESTINFO}
  _TV_HITTESTINFO = tagTVHITTESTINFO;
  TTVHitTestInfo = tagTVHITTESTINFO;
  {$EXTERNALSYM TV_HITTESTINFO}
  TV_HITTESTINFO = tagTVHITTESTINFO;

{$EXTERNALSYM TreeView_HitTest}
function TreeView_HitTest(hwnd: HWND; var lpht: TTVHitTestInfo): HTreeItem; inline;

const
  {$EXTERNALSYM TVHT_NOWHERE}
  TVHT_NOWHERE            = $0001;
  {$EXTERNALSYM TVHT_ONITEMICON}
  TVHT_ONITEMICON         = $0002;
  {$EXTERNALSYM TVHT_ONITEMLABEL}
  TVHT_ONITEMLABEL        = $0004;
  {$EXTERNALSYM TVHT_ONITEMINDENT}
  TVHT_ONITEMINDENT       = $0008;
  {$EXTERNALSYM TVHT_ONITEMBUTTON}
  TVHT_ONITEMBUTTON       = $0010;
  {$EXTERNALSYM TVHT_ONITEMRIGHT}
  TVHT_ONITEMRIGHT        = $0020;
  {$EXTERNALSYM TVHT_ONITEMSTATEICON}
  TVHT_ONITEMSTATEICON    = $0040;

  {$EXTERNALSYM TVHT_ONITEM}
  TVHT_ONITEM             = TVHT_ONITEMICON or TVHT_ONITEMLABEL or
			      TVHT_ONITEMSTATEICON;

  {$EXTERNALSYM TVHT_ABOVE}
  TVHT_ABOVE              = $0100;
  {$EXTERNALSYM TVHT_BELOW}
  TVHT_BELOW              = $0200;
  {$EXTERNALSYM TVHT_TORIGHT}
  TVHT_TORIGHT            = $0400;
  {$EXTERNALSYM TVHT_TOLEFT}
  TVHT_TOLEFT             = $0800;

const
  {$EXTERNALSYM TVM_CREATEDRAGIMAGE}
  TVM_CREATEDRAGIMAGE     = TV_FIRST + 18;

{$EXTERNALSYM TreeView_CreateDragImage}
function TreeView_CreateDragImage(hwnd: HWND; hitem: HTreeItem): HIMAGELIST; inline;

const
  {$EXTERNALSYM TVM_SORTCHILDREN}
  TVM_SORTCHILDREN        = TV_FIRST + 19;

{$EXTERNALSYM TreeView_SortChildren}
function TreeView_SortChildren(hwnd: HWND; hitem: HTreeItem;
  recurse: Integer): Bool; inline;

const
  {$EXTERNALSYM TVM_ENSUREVISIBLE}
  TVM_ENSUREVISIBLE       = TV_FIRST + 20;

{$EXTERNALSYM TreeView_EnsureVisible}
function TreeView_EnsureVisible(hwnd: HWND; hitem: HTreeItem): Bool; inline;

const
  {$EXTERNALSYM TVM_SORTCHILDRENCB}
  TVM_SORTCHILDRENCB      = TV_FIRST + 21;

type
  {$EXTERNALSYM PFNTVCOMPARE}
  PFNTVCOMPARE = function(lParam1, lParam2, lParamSort: Longint): Integer stdcall;
  TTVCompare = PFNTVCOMPARE;

type
  {$EXTERNALSYM tagTVSORTCB}
  tagTVSORTCB = packed record
    hParent: HTreeItem;
    lpfnCompare: TTVCompare;
    lParam: LPARAM;
  end;
  {$EXTERNALSYM _TV_SORTCB}
  _TV_SORTCB = tagTVSORTCB;
  TTVSortCB = tagTVSORTCB;
  {$EXTERNALSYM TV_SORTCB}
  TV_SORTCB = tagTVSORTCB;

{$EXTERNALSYM TreeView_SortChildrenCB}
function TreeView_SortChildrenCB(hwnd: HWND; const psort: TTVSortCB;
  recurse: Integer): Bool; inline;

const
  {$EXTERNALSYM TVM_ENDEDITLABELNOW}
  TVM_ENDEDITLABELNOW     = TV_FIRST + 22;

{$EXTERNALSYM TreeView_EndEditLabelNow}
function TreeView_EndEditLabelNow(hwnd: HWND; fCancel: Bool): Bool; inline;

const
  {$EXTERNALSYM TVM_GETISEARCHSTRINGA}
  TVM_GETISEARCHSTRINGA    = TV_FIRST + 23;
  {$EXTERNALSYM TVM_GETISEARCHSTRINGW}
  TVM_GETISEARCHSTRINGW    = TV_FIRST + 64;




  {$EXTERNALSYM TVM_GETISEARCHSTRING}
  TVM_GETISEARCHSTRING    = TVM_GETISEARCHSTRINGA;


{$EXTERNALSYM TreeView_GetISearchString}
function TreeView_GetISearchString(hwndTV: HWND; lpsz: PChar): Bool; inline;
{$EXTERNALSYM TreeView_GetISearchStringA}
function TreeView_GetISearchStringA(hwndTV: HWND; lpsz: PAnsiChar): Bool; inline;
{$EXTERNALSYM TreeView_GetISearchStringW}
function TreeView_GetISearchStringW(hwndTV: HWND; lpsz: PWideChar): Bool; inline;

const
  {$EXTERNALSYM TVM_SETTOOLTIPS}
  TVM_SETTOOLTIPS         = TV_FIRST + 24;

{$EXTERNALSYM TreeView_SetToolTips}
function TreeView_SetToolTips(wnd: HWND; hwndTT: HWND): HWND; inline;

const
  {$EXTERNALSYM TVM_GETTOOLTIPS}
  TVM_GETTOOLTIPS         = TV_FIRST + 25;

{$EXTERNALSYM TreeView_GetToolTips}
function TreeView_GetToolTips(wnd: HWND): HWND; inline;

const
  {$EXTERNALSYM TVM_SETINSERTMARK}
  TVM_SETINSERTMARK       = TV_FIRST + 26;

{$EXTERNALSYM TreeView_SetInsertMark}
function TreeView_SetInsertMark(hwnd: HWND; hItem: Integer; fAfter: BOOL): BOOL; inline;

const
  {$EXTERNALSYM TVM_SETUNICODEFORMAT}
  TVM_SETUNICODEFORMAT     = CCM_SETUNICODEFORMAT;

{$EXTERNALSYM TreeView_SetUnicodeFormat}
function TreeView_SetUnicodeFormat(hwnd: HWND; fUnicode: BOOL): BOOL; inline;

const
  {$EXTERNALSYM TVM_GETUNICODEFORMAT}
  TVM_GETUNICODEFORMAT     = CCM_GETUNICODEFORMAT;

{$EXTERNALSYM TreeView_GetUnicodeFormat}
function TreeView_GetUnicodeFormat(hwnd: HWND): BOOL; inline;

const
  {$EXTERNALSYM TVM_SETITEMHEIGHT}
  TVM_SETITEMHEIGHT         = TV_FIRST + 27;

{$EXTERNALSYM TreeView_SetItemHeight}
function TreeView_SetItemHeight(hwnd: HWND; iHeight: Integer): Integer; inline;

const
  {$EXTERNALSYM TVM_GETITEMHEIGHT}
  TVM_GETITEMHEIGHT         = TV_FIRST + 28;

{$EXTERNALSYM TreeView_GetItemHeight}
function TreeView_GetItemHeight(hwnd: HWND): Integer; inline;

const
  {$EXTERNALSYM TVM_SETBKCOLOR}
  TVM_SETBKCOLOR              = TV_FIRST + 29;

{$EXTERNALSYM TreeView_SetBkColor}
function TreeView_SetBkColor(hwnd: HWND; clr: COLORREF): COLORREF; inline;

const
  {$EXTERNALSYM TVM_SETTEXTCOLOR}
  TVM_SETTEXTCOLOR              = TV_FIRST + 30;

{$EXTERNALSYM TreeView_SetTextColor}
function TreeView_SetTextColor(hwnd: HWND; clr: COLORREF): COLORREF; inline;

const
  {$EXTERNALSYM TVM_GETBKCOLOR}
  TVM_GETBKCOLOR              = TV_FIRST + 31;

{$EXTERNALSYM TreeView_GetBkColor}
function TreeView_GetBkColor(hwnd: HWND): COLORREF; inline;

const
  {$EXTERNALSYM TVM_GETTEXTCOLOR}
  TVM_GETTEXTCOLOR              = TV_FIRST + 32;

{$EXTERNALSYM TreeView_GetTextColor}
function TreeView_GetTextColor(hwnd: HWND): COLORREF; inline;

const
  {$EXTERNALSYM TVM_SETSCROLLTIME}
  TVM_SETSCROLLTIME              = TV_FIRST + 33;

{$EXTERNALSYM TreeView_SetScrollTime}
function TreeView_SetScrollTime(hwnd: HWND; uTime: UINT): UINT; inline;

const
  {$EXTERNALSYM TVM_GETSCROLLTIME}
  TVM_GETSCROLLTIME              = TV_FIRST + 34;

{$EXTERNALSYM TreeView_GetScrollTime}
function TreeView_GetScrollTime(hwnd: HWND): UINT; inline;

const
  {$EXTERNALSYM TVM_SETINSERTMARKCOLOR}
  TVM_SETINSERTMARKCOLOR         = TV_FIRST + 37;

{$EXTERNALSYM TreeView_SetInsertMarkColor}
function TreeView_SetInsertMarkColor(hwnd: HWND; clr: COLORREF): COLORREF; inline;

const
  {$EXTERNALSYM TVM_GETINSERTMARKCOLOR}
  TVM_GETINSERTMARKCOLOR         = TV_FIRST + 38;

{$EXTERNALSYM TreeView_GetInsertMarkColor}
function TreeView_GetInsertMarkColor(hwnd: HWND): COLORREF; inline;

type
  PNMTreeViewA = ^TNMTreeViewA;
  PNMTreeViewW = ^TNMTreeViewW;
  PNMTreeView = PNMTreeViewA;
  {$EXTERNALSYM tagNMTREEVIEWA}
  tagNMTREEVIEWA = packed record
    hdr: TNMHDR;
    action: UINT;
    itemOld: TTVItemA;
    itemNew: TTVItemA;
    ptDrag: TPoint;
  end;
  {$EXTERNALSYM tagNMTREEVIEWW}
  tagNMTREEVIEWW = packed record
    hdr: TNMHDR;
    action: UINT;
    itemOld: TTVItemW;
    itemNew: TTVItemW;
    ptDrag: TPoint;
  end;
  {$EXTERNALSYM tagNMTREEVIEW}
  tagNMTREEVIEW = tagNMTREEVIEWA;
  {$EXTERNALSYM _NM_TREEVIEWA}
  _NM_TREEVIEWA = tagNMTREEVIEWA;
  {$EXTERNALSYM _NM_TREEVIEWW}
  _NM_TREEVIEWW = tagNMTREEVIEWW;
  {$EXTERNALSYM _NM_TREEVIEW}
  _NM_TREEVIEW = _NM_TREEVIEWA;
  TNMTreeViewA  = tagNMTREEVIEWA;
  TNMTreeViewW  = tagNMTREEVIEWW;
  TNMTreeView = TNMTreeViewA;
  {$EXTERNALSYM NM_TREEVIEWA}
  NM_TREEVIEWA  = tagNMTREEVIEWA;
  {$EXTERNALSYM NM_TREEVIEWW}
  NM_TREEVIEWW  = tagNMTREEVIEWW;
  {$EXTERNALSYM NM_TREEVIEW}
  NM_TREEVIEW = NM_TREEVIEWA;

const
  {$EXTERNALSYM TVN_SELCHANGINGA}
  TVN_SELCHANGINGA         = TVN_FIRST-1;
  {$EXTERNALSYM TVN_SELCHANGEDA}
  TVN_SELCHANGEDA          = TVN_FIRST-2;
  {$EXTERNALSYM TVN_SELCHANGINGW}
  TVN_SELCHANGINGW         = TVN_FIRST-50;
  {$EXTERNALSYM TVN_SELCHANGEDW}
  TVN_SELCHANGEDW          = TVN_FIRST-51;






  {$EXTERNALSYM TVN_SELCHANGING}
  TVN_SELCHANGING         = TVN_SELCHANGINGA;
  {$EXTERNALSYM TVN_SELCHANGED}
  TVN_SELCHANGED          = TVN_SELCHANGEDA;


const
  {$EXTERNALSYM TVC_UNKNOWN}
  TVC_UNKNOWN             = $0000;
  {$EXTERNALSYM TVC_BYMOUSE}
  TVC_BYMOUSE             = $0001;
  {$EXTERNALSYM TVC_BYKEYBOARD}
  TVC_BYKEYBOARD          = $0002;

const
  {$EXTERNALSYM TVN_GETDISPINFOA}
  TVN_GETDISPINFOA         = TVN_FIRST-3;
  {$EXTERNALSYM TVN_SETDISPINFOA}
  TVN_SETDISPINFOA         = TVN_FIRST-4;
  {$EXTERNALSYM TVN_GETDISPINFOW}
  TVN_GETDISPINFOW         = TVN_FIRST-52;
  {$EXTERNALSYM TVN_SETDISPINFOW}
  TVN_SETDISPINFOW         = TVN_FIRST-53;






  {$EXTERNALSYM TVN_GETDISPINFO}
  TVN_GETDISPINFO         = TVN_GETDISPINFOA;
  {$EXTERNALSYM TVN_SETDISPINFO}
  TVN_SETDISPINFO         = TVN_SETDISPINFOA;


  {$EXTERNALSYM TVIF_DI_SETITEM}
  TVIF_DI_SETITEM         = $1000;

type
  PTVDispInfoA = ^TTVDispInfoA;
  PTVDispInfoW = ^TTVDispInfoW;
  PTVDispInfo = PTVDispInfoA;
  {$EXTERNALSYM tagTVDISPINFOA}
  tagTVDISPINFOA = packed record
    hdr: TNMHDR;
    item: TTVItemA;
  end;
  {$EXTERNALSYM tagTVDISPINFOW}
  tagTVDISPINFOW = packed record
    hdr: TNMHDR;
    item: TTVItemW;
  end;
  {$EXTERNALSYM tagTVDISPINFO}
  tagTVDISPINFO = tagTVDISPINFOA;
  {$EXTERNALSYM _TV_DISPINFOA}
  _TV_DISPINFOA = tagTVDISPINFOA;
  {$EXTERNALSYM _TV_DISPINFOW}
  _TV_DISPINFOW = tagTVDISPINFOW;
  {$EXTERNALSYM _TV_DISPINFO}
  _TV_DISPINFO = _TV_DISPINFOA;
  TTVDispInfoA = tagTVDISPINFOA;
  TTVDispInfoW = tagTVDISPINFOW;
  TTVDispInfo = TTVDispInfoA;
  {$EXTERNALSYM TV_DISPINFOA}
  TV_DISPINFOA = tagTVDISPINFOA;
  {$EXTERNALSYM TV_DISPINFOW}
  TV_DISPINFOW = tagTVDISPINFOW;
  {$EXTERNALSYM TV_DISPINFO}
  TV_DISPINFO = TV_DISPINFOA;

const
  {$EXTERNALSYM TVN_ITEMEXPANDINGA}
  TVN_ITEMEXPANDINGA       = TVN_FIRST-5;
  {$EXTERNALSYM TVN_ITEMEXPANDEDA}
  TVN_ITEMEXPANDEDA        = TVN_FIRST-6;
  {$EXTERNALSYM TVN_BEGINDRAGA}
  TVN_BEGINDRAGA           = TVN_FIRST-7;
  {$EXTERNALSYM TVN_BEGINRDRAGA}
  TVN_BEGINRDRAGA          = TVN_FIRST-8;
  {$EXTERNALSYM TVN_DELETEITEMA}
  TVN_DELETEITEMA          = TVN_FIRST-9;
  {$EXTERNALSYM TVN_BEGINLABELEDITA}
  TVN_BEGINLABELEDITA      = TVN_FIRST-10;
  {$EXTERNALSYM TVN_ENDLABELEDITA}
  TVN_ENDLABELEDITA        = TVN_FIRST-11;
  {$EXTERNALSYM TVN_GETINFOTIPA}
  TVN_GETINFOTIPA          = TVN_FIRST-13;
  {$EXTERNALSYM TVN_ITEMEXPANDINGW}
  TVN_ITEMEXPANDINGW       = TVN_FIRST-54;
  {$EXTERNALSYM TVN_ITEMEXPANDEDW}
  TVN_ITEMEXPANDEDW        = TVN_FIRST-55;
  {$EXTERNALSYM TVN_BEGINDRAGW}
  TVN_BEGINDRAGW           = TVN_FIRST-56;
  {$EXTERNALSYM TVN_BEGINRDRAGW}
  TVN_BEGINRDRAGW          = TVN_FIRST-57;
  {$EXTERNALSYM TVN_DELETEITEMW}
  TVN_DELETEITEMW          = TVN_FIRST-58;
  {$EXTERNALSYM TVN_BEGINLABELEDITW}
  TVN_BEGINLABELEDITW      = TVN_FIRST-59;
  {$EXTERNALSYM TVN_ENDLABELEDITW}
  TVN_ENDLABELEDITW        = TVN_FIRST-60;
  {$EXTERNALSYM TVN_GETINFOTIPW}
  TVN_GETINFOTIPW          = TVN_FIRST-14;


















  {$EXTERNALSYM TVN_ITEMEXPANDING}
  TVN_ITEMEXPANDING       = TVN_ITEMEXPANDINGA;
  {$EXTERNALSYM TVN_ITEMEXPANDED}
  TVN_ITEMEXPANDED        = TVN_ITEMEXPANDEDA;
  {$EXTERNALSYM TVN_BEGINDRAG}
  TVN_BEGINDRAG           = TVN_BEGINDRAGA;
  {$EXTERNALSYM TVN_BEGINRDRAG}
  TVN_BEGINRDRAG          = TVN_BEGINRDRAGA;
  {$EXTERNALSYM TVN_DELETEITEM}
  TVN_DELETEITEM          = TVN_DELETEITEMA;
  {$EXTERNALSYM TVN_BEGINLABELEDIT}
  TVN_BEGINLABELEDIT      = TVN_BEGINLABELEDITA;
  {$EXTERNALSYM TVN_ENDLABELEDIT}
  TVN_ENDLABELEDIT        = TVN_ENDLABELEDITA;
  {$EXTERNALSYM TVN_GETINFOTIP}
  TVN_GETINFOTIP         = TVN_GETINFOTIPA;


const
  {$EXTERNALSYM TVN_KEYDOWN}
  TVN_KEYDOWN             = TVN_FIRST-12;
  {$EXTERNALSYM TVN_SINGLEEXPAND}
  TVN_SINGLEEXPAND        = TVN_FIRST-15;

type
  {$EXTERNALSYM tagTVKEYDOWN}
  tagTVKEYDOWN = packed record
    hdr: TNMHDR;
    wVKey: Word;
    flags: UINT;
  end;
  {$EXTERNALSYM _TV_KEYDOWN}
  _TV_KEYDOWN = tagTVKEYDOWN;
  TTVKeyDown = tagTVKEYDOWN;
  {$EXTERNALSYM TV_KEYDOWN}
  TV_KEYDOWN = tagTVKEYDOWN;

  {$EXTERNALSYM tagNMTVCUSTOMDRAW}
  tagNMTVCUSTOMDRAW = packed record
    nmcd: TNMCustomDraw;
    clrText: COLORREF;
    clrTextBk: COLORREF;
    iLevel: Integer;
  end;
  PNMTVCustomDraw = ^TNMTVCustomDraw;
  TNMTVCustomDraw = tagNMTVCUSTOMDRAW;

  // for tooltips
  {$EXTERNALSYM tagNMTVGETINFOTIPA}
  tagNMTVGETINFOTIPA = packed record
    hdr: TNMHdr;
    pszText: PAnsiChar;
    cchTextMax: Integer;
    hItem: HTREEITEM;
    lParam: LPARAM;
  end;
  {$EXTERNALSYM tagNMTVGETINFOTIPW}
  tagNMTVGETINFOTIPW = packed record
    hdr: TNMHdr;
    pszText: PWideChar;
    cchTextMax: Integer;
    hItem: HTREEITEM;
    lParam: LPARAM;
  end;
  {$EXTERNALSYM tagNMTVGETINFOTIP}
  tagNMTVGETINFOTIP = tagNMTVGETINFOTIPA;
  PNMTVGetInfoTipA = ^TNMTVGetInfoTipA;
  PNMTVGetInfoTipW = ^TNMTVGetInfoTipW;
  PNMTVGetInfoTip = PNMTVGetInfoTipA;
  TNMTVGetInfoTipA = tagNMTVGETINFOTIPA;
  TNMTVGetInfoTipW = tagNMTVGETINFOTIPW;
  TNMTVGetInfoTip = TNMTVGetInfoTipA;

const
  // treeview's customdraw return meaning don't draw images.  valid on CDRF_NOTIFYITEMPREPAINT
  {$EXTERNALSYM TVCDRF_NOIMAGES}
  TVCDRF_NOIMAGES         = $00010000;






implementation

const
{$IFDEF MSWINDOWS}
  cctrl = comctl32; { From Windows.pas }
{$ENDIF}


{ Image List }

function ImageList_Create; external cctrl name 'ImageList_Create';
function ImageList_Destroy; external cctrl name 'ImageList_Destroy';
function ImageList_GetImageCount; external cctrl name 'ImageList_GetImageCount';
function ImageList_SetImageCount; external cctrl name 'ImageList_SetImageCount';
function ImageList_Add; external cctrl name 'ImageList_Add';
function ImageList_ReplaceIcon; external cctrl name 'ImageList_ReplaceIcon';
function ImageList_SetBkColor; external cctrl name 'ImageList_SetBkColor';
function ImageList_GetBkColor; external cctrl name 'ImageList_GetBkColor';
function ImageList_SetOverlayImage; external cctrl name 'ImageList_SetOverlayImage';

function ImageList_AddIcon(ImageList: HIMAGELIST; Icon: HIcon): Integer;
begin
  Result := ImageList_ReplaceIcon(ImageList, -1, Icon);
end;

function IndexToOverlayMask(Index: Integer): Integer;
begin
  Result := Index shl 8;
end;

function ImageList_Draw; external cctrl name 'ImageList_Draw';

function ImageList_Replace; external cctrl name 'ImageList_Replace';
function ImageList_AddMasked; external cctrl name 'ImageList_AddMasked';
function ImageList_DrawEx; external cctrl name 'ImageList_DrawEx';
function ImageList_DrawIndirect; external cctrl name 'ImageList_DrawIndirect';
function ImageList_Remove; external cctrl name 'ImageList_Remove';
function ImageList_GetIcon; external cctrl name 'ImageList_GetIcon';
function ImageList_LoadImage; external cctrl name 'ImageList_LoadImageA';
function ImageList_LoadImageA; external cctrl name 'ImageList_LoadImageA';
function ImageList_LoadImageW; external cctrl name 'ImageList_LoadImageW';
function ImageList_Copy; external cctrl name 'ImageList_Copy';
function ImageList_BeginDrag; external cctrl name 'ImageList_BeginDrag';
function ImageList_EndDrag; external cctrl name 'ImageList_EndDrag';
function ImageList_DragEnter; external cctrl name 'ImageList_DragEnter';
function ImageList_DragLeave; external cctrl name 'ImageList_DragLeave';
function ImageList_DragMove; external cctrl name 'ImageList_DragMove';
function ImageList_SetDragCursorImage; external cctrl name 'ImageList_SetDragCursorImage';
function ImageList_DragShowNolock; external cctrl name 'ImageList_DragShowNolock';
function ImageList_GetDragImage; external cctrl name 'ImageList_GetDragImage';

{ macros }
procedure ImageList_RemoveAll(ImageList: HIMAGELIST);
begin
  ImageList_Remove(ImageList, -1);
end;

function ImageList_ExtractIcon(Instance: THandle; ImageList: HIMAGELIST;
  Image: Integer): HIcon;
begin
  Result := ImageList_GetIcon(ImageList, Image, 0);
end;

function ImageList_LoadBitmap(Instance: THandle; Bmp: PChar;
  CX, Grow: Integer; Mask: TColorRef): HIMAGELIST;
begin
  Result := ImageList_LoadImage(Instance, Bmp, CX, Grow, Mask,
    IMAGE_BITMAP, 0);
end;
function ImageList_LoadBitmapA(Instance: THandle; Bmp: PAnsiChar;
  CX, Grow: Integer; Mask: TColorRef): HIMAGELIST;
begin
  Result := ImageList_LoadImageA(Instance, Bmp, CX, Grow, Mask,
    IMAGE_BITMAP, 0);
end;
function ImageList_LoadBitmapW(Instance: THandle; Bmp: PWideChar;
  CX, Grow: Integer; Mask: TColorRef): HIMAGELIST;
begin
  Result := ImageList_LoadImageW(Instance, Bmp, CX, Grow, Mask,
    IMAGE_BITMAP, 0);
end;

function ImageList_Read; external cctrl name 'ImageList_Read';
function ImageList_Write; external cctrl name 'ImageList_Write';

function ImageList_GetIconSize; external cctrl name 'ImageList_GetIconSize';
function ImageList_SetIconSize; external cctrl name 'ImageList_SetIconSize';
function ImageList_GetImageInfo; external cctrl name 'ImageList_GetImageInfo';
function ImageList_Merge; external cctrl name 'ImageList_Merge';
function ImageList_Duplicate(himl: HIMAGELIST): HIMAGELIST; stdcall; external cctrl name 'ImageList_Duplicate';



{ Tree View }

function TreeView_InsertItem(hwnd: HWND; const lpis: TTVInsertStruct): HTreeItem;
begin
  Result := HTreeItem( SendMessage(hwnd, TVM_INSERTITEM, 0, Longint(@lpis)) );
end;
function TreeView_InsertItemA(hwnd: HWND; const lpis: TTVInsertStructA): HTreeItem;
begin
  Result := HTreeItem( SendMessageA(hwnd, TVM_INSERTITEMA, 0, Longint(@lpis)) );
end;
function TreeView_InsertItemW(hwnd: HWND; const lpis: TTVInsertStructW): HTreeItem;
begin
  Result := HTreeItem( SendMessageW(hwnd, TVM_INSERTITEMW, 0, Longint(@lpis)) );
end;

function TreeView_DeleteItem(hwnd: HWND; hitem: HTreeItem): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_DELETEITEM, 0, Longint(hitem)) );
end;

function TreeView_DeleteAllItems(hwnd: HWND): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_DELETEITEM, 0, Longint(TVI_ROOT)) );
end;

function TreeView_Expand(hwnd: HWND; hitem: HTreeItem; code: Integer): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_EXPAND, code, Longint(hitem)) );
end;

function TreeView_GetItemRect(hwnd: HWND; hitem: HTreeItem;
  var prc: TRect; code: Bool): Bool;
begin
  HTreeItem(Pointer(@prc)^) := hitem;
  Result := Bool( SendMessage(hwnd, TVM_GETITEMRECT, Integer(code), Longint(@prc)) );
end;

function TreeView_GetCount(hwnd: HWND): UINT;
begin
  Result := SendMessage(hwnd, TVM_GETCOUNT, 0, 0);
end;

function TreeView_GetIndent(hwnd: HWND): UINT;
begin
  Result := SendMessage(hwnd, TVM_GETINDENT, 0, 0);
end;

function TreeView_SetIndent(hwnd: HWND; indent: Integer): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_SETINDENT, indent, 0) );
end;

function TreeView_GetImageList(hwnd: HWND; iImage: Integer): HIMAGELIST;
begin
  Result := HIMAGELIST( SendMessage(hwnd, TVM_GETIMAGELIST, iImage, 0) );
end;

function TreeView_SetImageList(hwnd: HWND; himl: HIMAGELIST;
  iImage: Integer): HIMAGELIST;
begin
  Result := HIMAGELIST( SendMessage(hwnd, TVM_SETIMAGELIST, iImage,
    Longint(himl)) );
end;

function TreeView_GetNextItem(hwnd: HWND; hitem: HTreeItem;
  code: Integer): HTreeItem;
begin
  Result := HTreeItem( SendMessage(hwnd, TVM_GETNEXTITEM, code,
    Longint(hitem)) );
end;

function TreeView_GetChild(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_CHILD);
end;

function TreeView_GetNextSibling(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_NEXT);
end;

function TreeView_GetPrevSibling(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_PREVIOUS);
end;

function TreeView_GetParent(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_PARENT);
end;

function TreeView_GetFirstVisible(hwnd: HWND): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, nil,  TVGN_FIRSTVISIBLE);
end;

function TreeView_GetNextVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_NEXTVISIBLE);
end;

function TreeView_GetPrevVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, hitem, TVGN_PREVIOUSVISIBLE);
end;

function TreeView_GetSelection(hwnd: HWND): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, nil, TVGN_CARET);
end;

function TreeView_GetDropHilite(hwnd: HWND): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, nil, TVGN_DROPHILITE);
end;

function TreeView_GetRoot(hwnd: HWND): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, nil, TVGN_ROOT);
end;

function TreeView_GetLastVisible(hwnd: HWND): HTreeItem;
begin
  Result := TreeView_GetNextItem(hwnd, nil,  TVGN_LASTVISIBLE);
end;

function TreeView_Select(hwnd: HWND; hitem: HTreeItem;
  code: Integer): HTreeItem;
begin
  Result := HTreeItem( SendMessage(hwnd, TVM_SELECTITEM, code,
    Longint(hitem)) );
end;

function TreeView_SelectItem(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_Select(hwnd, hitem, TVGN_CARET);
end;

function TreeView_SelectDropTarget(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_Select(hwnd, hitem, TVGN_DROPHILITE);
end;

function TreeView_SelectSetFirstVisible(hwnd: HWND; hitem: HTreeItem): HTreeItem;
begin
  Result := TreeView_Select(hwnd, hitem, TVGN_FIRSTVISIBLE);
end;

function TreeView_GetItem(hwnd: HWND; var pitem: TTVItem): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_GETITEM, 0, Longint(@pitem)) );
end;
function TreeView_GetItemA(hwnd: HWND; var pitem: TTVItemA): Bool;
begin
  Result := Bool( SendMessageA(hwnd, TVM_GETITEMA, 0, Longint(@pitem)) );
end;
function TreeView_GetItemW(hwnd: HWND; var pitem: TTVItemW): Bool;
begin
  Result := Bool( SendMessageW(hwnd, TVM_GETITEMW, 0, Longint(@pitem)) );
end;

function TreeView_SetItem(hwnd: HWND; const pitem: TTVItem): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_SETITEM, 0, Longint(@pitem)) );
end;
function TreeView_SetItemA(hwnd: HWND; const pitem: TTVItemA): Bool;
begin
  Result := Bool( SendMessageA(hwnd, TVM_SETITEMA, 0, Longint(@pitem)) );
end;
function TreeView_SetItemW(hwnd: HWND; const pitem: TTVItemW): Bool;
begin
  Result := Bool( SendMessageW(hwnd, TVM_SETITEMW, 0, Longint(@pitem)) );
end;

function TreeView_EditLabel(hwnd: HWND; hitem: HTreeItem): HWND;
begin
  Result := Windows.HWND( SendMessage(hwnd, TVM_EDITLABEL, 0, Longint(hitem)) );
end;
function TreeView_EditLabelA(hwnd: HWND; hitem: HTreeItem): HWND;
begin
  Result := Windows.HWND( SendMessageA(hwnd, TVM_EDITLABELA, 0, Longint(hitem)) );
end;
function TreeView_EditLabelW(hwnd: HWND; hitem: HTreeItem): HWND;
begin
  Result := Windows.HWND( SendMessageW(hwnd, TVM_EDITLABELW, 0, Longint(hitem)) );
end;

function TreeView_GetEditControl(hwnd: HWND): HWND;
begin
  Result := Windows.HWND( SendMessage(hwnd, TVM_GETEDITCONTROL, 0, 0) );
end;

function TreeView_GetVisibleCount(hwnd: HWND): UINT;
begin
  Result := SendMessage(hwnd, TVM_GETVISIBLECOUNT, 0, 0);
end;

function TreeView_HitTest(hwnd: HWND; var lpht: TTVHitTestInfo): HTreeItem;
begin
  Result := HTreeItem( SendMessage(hwnd, TVM_HITTEST, 0, Longint(@lpht)) );
end;

function TreeView_CreateDragImage(hwnd: HWND; hitem: HTreeItem): HIMAGELIST;
begin
  Result := HIMAGELIST( SendMessage(hwnd, TVM_CREATEDRAGIMAGE, 0,
    Longint(hitem)) );
end;

function TreeView_SortChildren(hwnd: HWND; hitem: HTreeItem;
  recurse: Integer): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_SORTCHILDREN, recurse,
    Longint(hitem)) );
end;

function TreeView_EnsureVisible(hwnd: HWND; hitem: HTreeItem): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_ENSUREVISIBLE, 0, Longint(hitem)) );
end;

function TreeView_SortChildrenCB(hwnd: HWND; const psort: TTVSortCB;
  recurse: Integer): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_SORTCHILDRENCB, recurse,
    Longint(@psort)) );
end;

function TreeView_EndEditLabelNow(hwnd: HWND; fCancel: Bool): Bool;
begin
  Result := Bool( SendMessage(hwnd, TVM_ENDEDITLABELNOW, Integer(fCancel),
    0) );
end;

function TreeView_GetISearchString(hwndTV: HWND; lpsz: PChar): Bool;
begin
  Result := Bool( SendMessage(hwndTV, TVM_GETISEARCHSTRING, 0,
    Longint(lpsz)) );
end;
function TreeView_GetISearchStringA(hwndTV: HWND; lpsz: PAnsiChar): Bool;
begin
  Result := Bool( SendMessageA(hwndTV, TVM_GETISEARCHSTRINGA, 0,
    Longint(lpsz)) );
end;
function TreeView_GetISearchStringW(hwndTV: HWND; lpsz: PWideChar): Bool;
begin
  Result := Bool( SendMessageW(hwndTV, TVM_GETISEARCHSTRINGW, 0,
    Longint(lpsz)) );
end;

function TreeView_SetToolTips(wnd: HWND; hwndTT: HWND): HWND;
begin
  Result := HWND(SendMessage(wnd, TVM_SETTOOLTIPS, WPARAM(hwndTT), 0));
end;

function TreeView_GetToolTips(wnd: HWND): HWND;
begin
  Result := HWND(SendMessage(wnd, TVM_GETTOOLTIPS, 0, 0));
end;

function TreeView_SetInsertMark(hwnd: HWND; hItem: Integer; fAfter: BOOL): BOOL;
begin
  Result := BOOL(SendMessage(hwnd, TVM_SETINSERTMARK, WPARAM(fAfter), LPARAM(hItem)));
end;

function TreeView_SetUnicodeFormat(hwnd: HWND; fUnicode: BOOL): BOOL;
begin
  Result := BOOL(SendMessage(hwnd, TVM_SETUNICODEFORMAT, WPARAM(fUnicode), 0));
end;

function TreeView_GetUnicodeFormat(hwnd: HWND): BOOL;
begin
  Result := BOOL(SendMessage(hwnd, TVM_GETUNICODEFORMAT, 0, 0));
end;

function TreeView_SetItemHeight(hwnd: HWND; iHeight: Integer): Integer;
begin
  Result := SendMessage(hwnd, TVM_SETITEMHEIGHT, iHeight, 0);
end;

function TreeView_GetItemHeight(hwnd: HWND): Integer;
begin
  Result := SendMessage(hwnd, TVM_GETITEMHEIGHT, 0, 0);
end;

function TreeView_SetBkColor(hwnd: HWND; clr: COLORREF): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_SETBKCOLOR, 0, LPARAM(clr)));
end;

function TreeView_SetTextColor(hwnd: HWND; clr: COLORREF): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_SETTEXTCOLOR, 0, LPARAM(clr)));
end;

function TreeView_GetBkColor(hwnd: HWND): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_GETBKCOLOR, 0, 0));
end;

function TreeView_GetTextColor(hwnd: HWND): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_GETTEXTCOLOR, 0, 0));
end;

function TreeView_SetScrollTime(hwnd: HWND; uTime: UINT): UINT;
begin
  Result := SendMessage(hwnd, TVM_SETSCROLLTIME, uTime, 0);
end;

function TreeView_GetScrollTime(hwnd: HWND): UINT;
begin
  Result := SendMessage(hwnd, TVM_GETSCROLLTIME, 0, 0);
end;

function TreeView_SetInsertMarkColor(hwnd: HWND; clr: COLORREF): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_SETINSERTMARKCOLOR, 0, LPARAM(clr)));
end;

function TreeView_GetInsertMarkColor(hwnd: HWND): COLORREF;
begin
  Result := COLORREF(SendMessage(hwnd, TVM_GETINSERTMARKCOLOR, 0, 0));
end;



end.